package task2;

import java.util.Random;

public class Consumer implements Runnable {

	private int _ID;	
	private CyclicBufferWithLock<String> buffer;

    public Consumer(int ID, CyclicBufferWithLock<String> buffer) {
        this._ID = ID;
    	this.buffer = buffer;
    }

    public void run() {
        Random random = new Random();        
        try {
        	for (String message = buffer.retrieveString();
        			!message.contains("DONE"); message = buffer.retrieveString()) {        		
        		System.out.println("CONSUMER " + _ID + " - MESSAGE RECEIVED: " + message);        		
        		try {
        			Thread.sleep(random.nextInt(1000));
        		} catch (InterruptedException e) { }
        	}
        } catch (InterruptedException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
        }
    }
}